using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class BulletManager: DrawableGameComponent
    {
        private static List<Bullet> _bullets = new List<Bullet>();  
       
        private static List<Bullet> _deletedBullets = new List<Bullet>();

        public BulletManager(Game game) : base(game)
        {
        }

        /// <summary>
        /// Them 1 doi tuong bullet
        /// </summary>
        /// <param name="bullets"></param>
        public void Add(Bullet bullet)
        {
            _bullets.Add(bullet);
        }

        /// <summary>
        /// Remove 1 doi tuong bullet, khi bullet da trung dich
        /// </summary>
        /// <param name="bullet"></param>
        public void Remove(Bullet bullet)
        {
            _deletedBullets.Add(bullet);
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            foreach (var deletedBullet in _deletedBullets)
            {
                _bullets.Remove(deletedBullet);
            }
            _deletedBullets.Clear();
            foreach (var bullet in _bullets)
            {
                bullet.Update(gameTime);
            }            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            foreach (var bullet in _bullets)
            {
                bullet.Draw(gameTime);
            }            
        }
    }
}
